let listBehavior = require('../../behavior/list')

Page({
  behaviors: [listBehavior,],
  data: {
    dropdownOption1: [
      { text: '当天订单', value: 0 },
      { text: '全部订单', value: 1 },
      { text: '预约订单', value: 2 },
    ],
    dropdownOption2: [
      { text: '系统推荐', value: 0 },
      { text: '整单利润最大', value: 1 },
      { text: '运输距离最短', value: 2 },
      { text: '运输时间最短', value: 3 },
    ],
    dropdownValue1: 1,// 默认全部订单，当天订单排在最前
    dropdownValue2: 0,
    dropdownValue3: 0,
    activeIndex: 0,
    dropdownOption3: [
      { text: '系统推荐', value: 0 },
      { text: '收泥尾款最高', value: 1 },
      { text: '付泥尾款最低', value: 2 },
      { text: '距离最近', value: 3 },
      { text: '时间最少', value: 4 },
      { text: '司机自选', value: 5 },
    ],
    page: 1,
    total: 0,
    limit: 10,
    list: [],
    dingdanzhuangtai: '下单',// 下单 抢单 完成
  },
  onLoad (option) {
    // this.getXiaoNaChangList();
    this.getCailiaozhongleiList();
  },
  onShow () {
    this.getFirstPage();
    this.setData({
      userName: wx.getStorageSync('user_name'),
      driver_car_name: wx.getStorageSync('driver_car_name'),
      nowData: wx.$util.formatYMD(new Date())
    })
  },
  getDataList () {
    let total = 0, obj = {};
    let { page, limit, filter, list, query, dingdanzhuangtai } = this.data;
    let resquestData = { "dingdanzhuangtai_list": dingdanzhuangtai, "page.no": page, "page.size": limit, "is_used": true }
    // let resquestData = { "page.no": page, "page.size": limit }
    if (this.data.activeIndex == 1 || this.data.activeIndex == 2) {
      resquestData.qiangdansiji = wx.getStorageSync('driver_row_id')
    }
    wx.showLoading({ title: '加载中', icon: "none" });
    wx.$get(5, 'ZTAuth/WasteOrder', resquestData).then(res => {
      wx.hideLoading();
      console.log(res, '？')
      if (res.response.errcode != 0) return wx.$toast(res.response.errmsg)
      if (res.data.row) {
        total = res.data.total;
        let rows = res.data.row;
        if (page !== 1) rows = [...list, ...rows,]
        obj = { total, list: rows, page };
      }
      this.setData(obj);
      console.log(obj, 'obj', this.data)
    })
  },
  getXiaoNaChangList () {
    let url = 'DynamicQuery/query_dynamic_v3',
      post = { "oby": "e_lajidingdan_xiaonachang_jijiabiao.row_id", "table_id": "e_lajidingdan_xiaonachang_jijiabiao" };
    post.SearchWhere = { filters: [], type: 'or' };
    wx.$post(3, url, post).then(res => {
      if (res.response.errcode != 0) return wx.$toast(res.response.errmsg)
      if (res.data.row) {
        let row = res.data.row, arrAy = {};
        let showRow = res.data.row.length > 3 ? res.data.row.slice(0, 3) : res.data.row
        row.map(element => {
          arrAy[element.xiaonachang] = element;
        });
        this.setData({ xiaonachangList: showRow, xiaonachangObj: arrAy })
      }
    })
  },
  getCailiaozhongleiList () {
    let url = 'DynamicQuery/query_dynamic_v3',
      post = { "oby": "e_jichuziliao_cailiaozhonglei.row_id", "table_id": "e_jichuziliao_cailiaozhonglei" };
    post.SearchWhere = { filters: [], type: 'or' };
    wx.$post(3, url, post).then(res => {
      if (res.response.errcode != 0) return wx.$toast(res.response.errmsg)
      if (res.data.row) {
        let row = res.data.row, arrAy = {};
        row.map(element => {
          arrAy[element.row_id] = element;
        });
        this.setData({ cailiaozhongleiList: arrAy })
      }
    })
  },
  openLocation () {
    // 打开地图查看位置
    let locationObj = {
      latitude: '',
      longitude: '',
      name: '',
      address: ''
    }
    wx.openLocation({
      ...locationObj,
      complete (res) {
        console.log(res, '查看信息呢')
      }
    })
  },
  showPicker () {
    this.setData({ showPicker: !this.data.showPicker })
  },
  onChangeTabs (e) {
    let dingdanzhuangtai = null;
    if (e.detail.index == 0) {
      dingdanzhuangtai = '下单'
    } else if (e.detail.index == 1) {
      dingdanzhuangtai = '抢单,报价,预付款,出车,装货,补录费用,支付余款,发车,缺货'
    } else if (e.detail.index == 2) {
      dingdanzhuangtai = '完成'
    } else {
      dingdanzhuangtai = ''
    }
    this.setData({
      activeIndex: e.detail.index,
      dingdanzhuangtai,
      page: 1,
      total: 0,
      limit: 10,
      list: [],
    }, () => {
      if (dingdanzhuangtai != '') {
        this.getDataList()
      }
    })
  },
  navPushPage (e) {
    let { row_id, activerow_id } = e.currentTarget.dataset, that = this;
    wx.navigateTo({
      url: '/SlagCar/GrabOrders/GrabOrders',
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        let obj = {};
        that.data.list.map(element => {
          if (element.row_id == row_id) {
            obj = element
          }
        });
        res.eventChannel.emit('acceptDataFromOpenerPage', {
          activeXiaoList: activerow_id || '',
          activeRowItem: obj,
          xiaonachangObj: that.data.xiaonachangObj,
          cailiaozhongleiList: that.data.cailiaozhongleiList
        })
      }
    })
  }
})